<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
  <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1">
  <meta name="referrer" content="never">
  <title>缓动动画</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    ul {
      list-style: none;
    }
    .box{
      position: absolute;
      top: 50px;
      left: 50px;
      width: 150px;
      height: 150px;
      background-color: #f00;
    }
  </style>
</head>
<body>
  <div class="box"></div>

  <script type="text/javascript">
    // 缓动动画：动画效果是运动速度逐渐降低的过程  
    // 公式求步长：step = (end - start) / 10

    var box = document.querySelector(".box");

    var flag = true;
    box.onclick = function (){
      var end = 600;

      var timer = setInterval(function (){
        // 获取步长
        var step = (end - box.offsetLeft) / 10;

        // 修正步长 如果终点与起点距离<10，将步长改为1，总可以到达终点
        if(Math.abs(step) < 1){ // 区间(-1,1)
          // step = step > 0 ? 1 : Math.floor(step);
          step = step < 0 ? -1 : Math.ceil(step);
        }

        // 运动
        box.style.left = box.offsetLeft + step + "px";

        // 停止条件
        if(box.offsetLeft == end){
          clearInterval(timer);
        }
      }, 1000/60);
    }
  </script>
</body>
</html>